<?php
/**
 * Created by PhpStorm.
 * User: hjlan
 * Date: 2016/5/6
 * Time: 17:17
 * 打印后台Service
 *
 */

namespace App\Services\PrintService;


use App\Jobs\Php2PyImg;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\DB;

class PrintBackService
{
    /**
     *
     * PrintBackService constructor.
     */
    public function __construct()
    {
    }
    public function rebuildProductImgByProductId($product_id)
    {
        try{
            if(empty($product_id)){
                return false;
            }
            $products=array();
            $products['product_id']=$product_id;
            $product_print_materials=array();
            $product_prints=$this->getProductPrintsByProductId($product_id);
            $product_prints_arr=array();
            if(empty($product_prints)){
                Log::info('rebuildProductImgByProductId  product_prints为空');
            return false;
            }
            foreach ($product_prints as $product_print){
                $product_print_id=$product_print->product_print_id;
                $position_type=$product_print->position_type;
                $print['product_print_id']=$product_print_id;
                $print['position_type']=$position_type;
                $product_print_materials_ids=$this->getProductPrintMaterialIdsByProductId($product_print_id);
                $print['product_print_materials_ids']=$product_print_materials_ids;
                $product_preview_image_id=$this->getProductPreviewImageIdByProductId($product_id,$position_type);
                if(empty($product_preview_image_id)){
                    Log::info('rebuildProductImgByProductId  $product_preview_image_id 为空');
                    continue;
                }
                $print['product_preview_image_id']=$product_preview_image_id;
                $product_prints_arr[]=$print;
            }
            $product['product_id']=$product_id;
            $product['product_print_materials']=$product_prints_arr;
            $products[0]=$product;
            dispatch(new Php2PyImg($products));
            return true;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }

    /***
     * 产品Id获取打印公共参数
     * @param $product_id
     * @return array|bool
     */
    public function getProductPrintsByProductId($product_id)
    {
        try{
            if(empty($product_id)){
                return array();
            }
            $product_prints=DB::table('product_prints')
                ->where(array('product_id'=>$product_id,'state'=>1))
                ->select('id as product_print_id','position_type')
                ->get()
                ->toArray();
            return empty($product_prints)?array():$product_prints;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }
    public function getProductPrintMaterialIdsByProductId($product_print_id)
    {
        try{
            if(empty($product_print_id)){
                return array();
            }
            $product_print_material_ids=DB::table('product_print_materials')
                ->where(array('product_print_id'=>$product_print_id,'state'=>1))
                ->pluck('id')
                ->toArray();
            return empty($product_print_material_ids)?array():$product_print_material_ids;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }
    public function getProductPreviewImageIdByProductId($product_id,$position_type)
    {
        try{
            if(empty($product_id)||empty($position_type)){
                return null;
            }
            $product_preview_image_id=DB::table('product_preview_images')
                ->where(array('position_type'=>$position_type,'state'=>1,'product_id'=>$product_id))
                ->value('product_preview_image_id');
            return $product_preview_image_id;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }
}